package com.topisv.tms.task.dispatch;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.topisv.tms.entity.task.DispatchOrderTask;
import com.topisv.tms.service.task.DispatchOrderTaskService;
import com.topisv.tms.utils.InitProperties;

/**
 * 发货单任务提供类
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2015-5-8      CC            1.0           Create
 */
@Component
@Lazy(false)
public class DispatchOrderTaskProvider implements InitializingBean {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private BlockingQueue<DispatchOrderTask> tasks = new ArrayBlockingQueue<DispatchOrderTask>(1000);
	@Autowired
	private DispatchOrderTaskService service;

	@Scheduled(cron = "0/30 * * * * *")
	public void executor() {
		try {
			service.lockData();
			List<DispatchOrderTask> data = service.findData();
			for (DispatchOrderTask task : data) {
				tasks.put(task);
			}
		} catch (Exception e) {
			logger.error("DispatchOrderTaskProvider case some error.", e);
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int i = 0; i < Integer.valueOf(InitProperties.get("dispatchorder.workthread.num").toString()); i++) {
			new DispatchOrderTaskWorker(tasks, service).start();
		}
	}
}
